﻿using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using Newtonsoft.Json;
using T.FAS.Runtime.Metadata.Entity.DataModel;
using T.ServiceComponent.DAC.Service;
using T.FAS.Runtime.Base.Entity;
using T.FAS.Runtime.Base.Dao;

namespace T.FAS.Runtime.DataModel.Dao
{
    /// <summary>
    /// CNO ELB数据访问类
    /// </summary>
    public class AssociationRefElementELBDao : DaoService<AssociationRefElement>
    {
        public override string TableName => "FAS_AssociationRefElement";
        public override string IDColumnName => "ID";
        public  string ForeignColumn => "AssociationID";
        public string MetaDataForeignColumn => "CompositeDataObjectID";

        public AssociationRefElementELBDao() : base()
        {
            DBName = "TFAS";
        }

        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<AssociationRefElement> GetAssociationRefElements(string assoId)
        {
            var result = new List<AssociationRefElement>();
            if (string.IsNullOrEmpty(assoId))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{ForeignColumn}='{assoId}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as AssociationRefElement));
            }
            return result;
        }
       
        /// <summary>
        /// 获取元数据内容
        /// </summary>
        /// <param name="metaDataId"></param>
        /// <returns></returns>
        public List<AssociationRefElement> GetAssociationRefElementsByMetaDataForeignKey(string metaDataID)
        {
            var result = new List<AssociationRefElement>();
            if (string.IsNullOrEmpty(metaDataID))
            {
                return result;
            }
            List<object> objects = base.GetObjectsByFilter($"{MetaDataForeignColumn}='{metaDataID}'");
            if (objects != null)
            {
                objects.ForEach(obj => result.Add(obj as AssociationRefElement));
            }
            return result;
        }

        /// <summary>
        /// 添加AssociationRefElement
        /// </summary>
        /// <param name="assoRefElements"></param>
        public void CreateAssociationRefElement(List<AssociationRefElement> assoRefElements)
        {
            try
            {
                base.Add(assoRefElements);
            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.CreateAssoRefError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除AssociationRefElement
        /// </summary>
        /// <param name="AssociationRefElements"></param>
        public void DeleteAssociationRefElement(string assoId)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.ForeignColumn, assoId));

            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.DeleteAssoRefError, e.Message, e);
            }
        }

        /// <summary>
        /// 删除AssociationRefElement
        /// </summary>
        /// <param name="metaDataID"></param>
        public void DeleteByMetaDataForeignKey(string metaDataID)
        {
            try
            {
                base.DB.ExecSqlStatement(string.Format(DaoUtil.DeleteSQLWithNoParam, this.TableName, this.MetaDataForeignColumn, metaDataID));
            }
            catch (Exception e)
            {

                throw new FasException(DataModelErrorCode.CDO.DeleteAssoRefError, e.Message, e);
            }
        }

    }
}
